package com.chen.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.entity.Category;
import com.chen.entity.Dish;
import com.chen.entity.Setmeal;
import com.chen.exception.DeleteException;
import com.chen.mapper.CategoryMapper;
import com.chen.service.CategoryService;
import com.chen.service.DishService;
import com.chen.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    @Override
    public Page<Category> page1(int page, int pageSize) {
        Page<Category> page1 = new Page<>(page, pageSize);
        page(page1);
        return page1;
    }

    @Override
    public void delete(long id) {
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Dish::getCategoryId, id);
        int count = dishService.count(lqw);
        LambdaQueryWrapper<Setmeal> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(Setmeal::getCategoryId, id);
        int count1 = setmealService.count(lqw1);
        if (count == 0 && count1 == 0) {
            removeById(id);
            return;
        }
        throw new DeleteException();
    }

    @Override
    public List<Category> getByType(int type) {
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Category::getType, type);
        return list(lqw);
    }

    @Override
    public List<Category> get(Category category) {
        Integer type = category.getType();
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.eq(type!=null,Category::getType,type);
        return list(lqw);
    }
}